<?php
class ListItemForm extends Form
{
    function ListItemForm()
    {
        Form::Form('ListItemForm');
        //$this->link_js('javascript/jquery/packed/jquery.livequery.js');

        $this->link_css('style/listItem.css');
        $this->link_header("<script>MM_preloadImages('style/images/add_uncheck.gif','style/images/add_check.gif');</script>");
        $this->id_vip = '';

        if (User::is_az_team()) {
            $this->link_js('javascript/enbac/items_list.js');
        }
    }

    function draw()
    {
        global $display;

        if (CGlobal::$curCategory) {
            $page_no = (int)Url::get('page_no', 1);

            if ($page_no <= 0) {
                $page_no = 1;
            }

            if (User::have_permit(ADMIN_ITEM)) {
                $subDir = 'list_item_admin';
            } else {
                $subDir = 'list_item';
            }

            //if(StaticCache::notExistCache('c'.CGlobal::$curCategory.'_'.$page_no,30,false,$subDir)){
            if (StaticCache::notExistCache('c' . CGlobal::$curCategory . '_' . $page_no, 1, FALSE, $subDir)) {
                StaticCache::startCache();
                $this->draw_from();
                StaticCache::endCache();
            }

            if (User::have_permit(ADMIN_ITEM)) {
                echo '<script>
						jQuery(document).ready(function(){		
							jQuery("div.admin_button").show();
							jQuery(".mod_menu").show();
						});
						
						function check_permit(catid){
							return true;
						}
					</script>';
            }
        } else {
            Url::redirect_url(WEB_DIR, 301);
        }
    }

    function draw_from()
    {
        //mb_internal_encoding("UTF-8");
        //AZLib::getCats();

        AZLib::getProvinces();

        global $display;
        $page_no = (int)Url::get('page_no', 1);

        if ($page_no <= 0) {
            $page_no = 1;
        }

        $admin_item = FALSE;

        if (User::have_permit(ADMIN_ITEM)) {
            $admin_item = TRUE;
        }


        $item_per_page = 30;

        $sql = 'SELECT * FROM item WHERE ';


        $where = '';

        $curMainCat   = CGlobal::$curMainCat;
        $curLevel2Cat = CGlobal::$curLevel2Cat;

        /*if($curLevel2Cat){
              $where		.=($where!=''?" AND ":"")." level_2_catid = ".$curLevel2Cat['id'];
          }
          elseif($curMainCat){
              $where		.=($where!=''?" AND ":"")." level_1_catid = ".$curMainCat['id'];
          }

          $where	.= ($where!=''?" AND ":"")." status=1";

          $sql		.= "$where ORDER BY item_order ASC";*/
        if (!CGlobal::$item_condition) {
            //Ngannv hack code trường hợp khong add module FilterMenu hoặc module Build
            if (isset($_GET['category_id']) && $_GET['category_id']) {
                CGlobal::$item_condition = " category_id = ".CGlobal::$curCategory." ";
            }

        }
        $sql .= CGlobal::$item_condition . " ORDER BY item_order ASC";

        $total_row = DB::count("item", CGlobal::$item_condition);

        if (CGlobal::$allCategories[CGlobal::$curCategory]['parent_id'] && isset(CGlobal::$allCategories[CGlobal::$allCategories[CGlobal::$curCategory]['parent_id']])) //DM cấp 2
        {
            $ebname = CGlobal::$allCategories[CGlobal::$allCategories[CGlobal::$curCategory]['parent_id']]['ebname'] . '-' . CGlobal::$allCategories[CGlobal::$curCategory]['ebname'];
        } else {
            $ebname = CGlobal::$allCategories[CGlobal::$curCategory]['ebname'];
        }


        #######################################################################################
        #Paging
        $url_new    = "?page=list_detail&category_id=" . CGlobal::$curCategory . "&ebname=" . $ebname;
        $url_addend = "";

        $filter_get = Url::get('filter');
        $min        = Url::get('min');
        $max        = Url::get('max');
        $order      = Url::get('order');

        if ($filter_get) {
            foreach ($filter_get as $get_gid => $get_fid) {
                $url_addend .= ($url_addend != '' ? '&' : '?') . "filter[{$get_gid}]=$get_fid";
            }
        }

        if ($min) {
            $url_addend .= ($url_addend != '' ? '&' : '?') . "min=$min";
        }

        if ($max) {
            $url_addend .= ($url_addend != '' ? '&' : '?') . "max=$max";
        }

        if ($order) {
            $url_addend .= ($url_addend != '' ? '&' : '?') . "order=$order";
        }
        $limit  = '';
        $paging = AZPagging::paging_list($limit, $total_row, $item_per_page, 10, 'page_no', $url_new, TRUE, 'Sản phẩm', 'Trang', $url_addend); //$paging = AZPagging::paging_list($limit,$item_per_page,'page_no',$url_new);

        $sql .= $limit;
        #Paging
        #######################################################################################


        $items = array();

        $re = DB::query($sql);


        if ($re) {
            while ($item = mysql_fetch_assoc($re)) {
                $items[$item['id']] = $this->processItem($item, $admin_item);
            }
        }

        $display->add('admin_item', $admin_item);
        $display->add('page', $page_no);
        $display->add('items', $items);
        $display->add('paging', $paging);

        $display->output("ListItem");
    }

    function processItem($item, $admin_item)
    {
        if (isset(CGlobal::$allCategories[$item['category_id']])) {
            $item['href'] = WEB_DIR . CGlobal::$allCategories[$item['category_id']]['nice_name'] . "/p{$item['id']}/" . AZLib::safe_title($item['name']) . ".html";
        } else {
            $item['href'] = WEB_DIR . "p{$item['id']}/" . AZLib::safe_title($item['name']) . ".html";
        }

        $item['modify_time'] = date('H:i | d.m.y', $item['modify_time']);

        $item['price']       = AZLib::convertCurrency($item['price'], $item['currency_id']);
        $item['price_out']   = AZLib::convertCurrency($item['price_out'], $item['currency_id']);
        $item['currency_id'] = 1;

        $item['price_num'] = (int)$item['price'];

        if ($item['price']) {
            $item['price'] = number_format($item['price'], 0, ',', '.');
        } else {
            $item['price'] = '';
        }

        $item['price_title'] = 'Giá bán';

        if ($item['price_out'] > 0) {
            $item['price_out'] = number_format($item['price_out'], 0, ',', '.') . ($item['currency_id'] == ' ' ? ' ' . CGlobal::$currency[$item['currency_id']] : '');
        } else {
            $item['price_out'] = '0';
        }

        //$item['price'] 			= AZLib::convertCurrency($item['price'],			$item['currency_id']);
        //$item['price_out'] 		= AZLib::convertCurrency($item['price_out'],		$item['currency_id']);


        if ($admin_item) {
            $item['description'] = AZLib::remove_4_js(AZLib::plainText(AZLib::parseBBCode(html_entity_decode($item['description'], ENT_QUOTES, "UTF-8"))));
            $item['description'] = preg_replace("/\\[([\s]*[0-9]{1,2}[\s]*)\]/eis", " ", $item['description']);
            $item['description'] = str_replace(array("'", "\""), array("", " "), $item['description']);
            $item['description'] = AZLib::word_limit($item['description'], 200, '');
        }

        $item['name'] = AZLib::filter_title($item['name']);

        if ($item['img_url']) {
            $item['img_thumb_wl'] = AZLib::getImageThumb($item['img_url'], 180, 0, 1, $item['img_server']);
        } else {
            $item['img_url'] = '';
        }

        if (!CGlobal::$curLevel2Cat) {
            if (isset(CGlobal::$allCategories[$item['category_id']])) {
                $item['cat_name'] = CGlobal::$allCategories[$item['category_id']]['name'];
            } else {
                $item['cat_name'] = "ID: {$item['category_id']}";
            }
        }

        return $item;
    }
}

?>